package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.ICompanyService;
import com.cn.wanxi.util.IO;
import com.cn.wanxi.util.Path;
import com.cn.wanxi.util.PathEnum;

/**
 * 服务逻辑层：所有封装的代码逻辑
 * * 1.创建文件
 * * 2.写入数据--
 * * 3.读取文件里面的数据---
 * * 4.根据读取的数据，封装成页面需要的数据
 */
public class CompanyServiceImpl implements ICompanyService {




    @Override
    public CompanyModel getCompanyModel(String path) {

//        1.创建文件
//        需要指定文件的路径和文件名

//        IO io = new IO();
//        File directory = IO.createDirectory(path, newDirectory);
        IO.createFile(path, Path.newDirectory, Path.FILE_NAME_COMPANY);
//        2.写入数据
//        String[] data = null;
//        2.5封装dao层数据
        CompanyDao companyDao = new CompanyDao();
        String[] data = {companyDao.logo, CompanyDao.phone, CompanyDao.net, CompanyDao.email, CompanyDao.address, CompanyDao.version, CompanyDao.linePhone};
        IO.wireteData(data);
//        3.读取数据
        String result = IO.readData();
//        4.封装数据
        return setData(result);
//        return null;
    }

    /**
     * 根据读取出来的数据封装成对象
     *
     * @param result
     * @return
     */
    private CompanyModel setData(String result) {
//      需要将result强制转换为我们需要的数据，
//        所以我们需要将result转换为数组的形式
//        result是字符串，如果转换为数组？
//        System.out.println(result);
        String[] split = result.split(Path.SPLIT);
//        for (int i = 0; i < split.length; i++) {
//            System.out.println(split[i]);
//        }
//        for (String aa : split
//        ) {
//            System.out.println(aa);
//        }
//        可不可以在封装的时候将乱码的字符串转换为UTF-8的形式？

        CompanyModel companyModel = new CompanyModel();

        companyModel.setLogo(split[0]);
        companyModel.setPhone(split[1]);
        companyModel.setNet(split[2]);
        companyModel.setEmail(split[3]);
        companyModel.setAddress(split[4]);
        companyModel.setVersion(split[5]);
        companyModel.setLinePhone(split[6]);
//        字符串編碼格式轉換
//        new String(split[4].getBytes(), StandardCharsets.UTF_8)
        return companyModel;
    }

//    /**
//     * 根据路径读取文件里面的数据
//     *
//     * @param file
//     * @return
//     */
//    private String readData(File file) {
//        StringBuilder data = new StringBuilder();
////        String和StringBuilder和StringBuffer的区别？
////        String a="aa";
////        String b="bb";
////        String c=a+b;
////        String d="aa"+"bb";
////        c.equals(d)为true和为false?
////        c和d分别创建了几个对象？
////        String e=new String("aa");
////        e和a是否相等？
////        字符流和字节流的区别?
//        try {
////            InputStream inputStream = new FileInputStream(file);
////
//////            循环遍历,要遍历多少次？
//////            int ---char
////            long count = file.length();
////            for (int i = 0; i < count; i++) {
////                System.out.print((char) inputStream.read());
////            }
////            需要用字符流读
//            InputStream is = new FileInputStream(file);
//            InputStreamReader reader = new InputStreamReader(is, StandardCharsets.UTF_8);
////            Reader reader = new FileReader(file);
//            int result = 0;
//
//            while ((result = reader.read()) != -1) {
////                result = reader.read();
////                System.out.print((char) result);
////                data += (char) result + "";
//                data.append((char) result);
//            }
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
////        System.out.println(data);
//        return data.toString();
//    }

//    /**
//     * 根据文件和数据进行写入
//     *
//     * @param file
//     * @param data
//     */
//    private void wireteData(File file, String[] data) {
//        try {
//            OutputStream outputStream = new FileOutputStream(file);
////            CompanyDao companyDao = new CompanyDao();
//            for (int i = 0; i < 111; i++) {
//                outputStream.write(data[i].getBytes(StandardCharsets.UTF_8));
//                outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
//            }
////            outputStream.write(data.toString().getBytes(StandardCharsets.UTF_8));
////            outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(CompanyDao.phone.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(CompanyDao.net.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(CompanyDao.email.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(CompanyDao.address.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(CompanyDao.version.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(SPLIT.getBytes(StandardCharsets.UTF_8));
////            outputStream.write(CompanyDao.linePhone.getBytes(StandardCharsets.UTF_8));
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//    }


}
